package com.javasoft.service.WJC.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasoft.dao.WJC.ExamQuestionMappingRepository;
import com.javasoft.dao.WJC.ExamRepository;
import com.javasoft.dao.WJC.PaperRuleDao;
import com.javasoft.dao.WJC.PaperRuleRepository;
import com.javasoft.dao.XGX.QuestionDao;
import com.javasoft.entity.WJC.Exam;
import com.javasoft.entity.WJC.paperRuleEntity;
import com.javasoft.service.WJC._interface.PaperRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service("paperRuleService")
public class PaperRuleServiceImpl extends ServiceImpl<PaperRuleDao, paperRuleEntity> implements PaperRuleService {
    @Autowired
    private PaperRuleDao paperRuleDao;
    @Autowired
    private ExamQuestionMappingRepository examQuestionMappingRepository;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private PaperRuleRepository paperRuleRepository;
    @Autowired
    private ExamRepository examRepository;

    @Override
    public Integer addPaperRule(paperRuleEntity paperRule) {
        paperRule.setCreatTime(LocalDateTime.now());
        return baseMapper.insert(paperRule);
    }

    @Override
    public boolean deletePaperRule(long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public Integer updatePaperRule(paperRuleEntity updatedPaperRuleEntity) {
        return baseMapper.updateById(updatedPaperRuleEntity);
    }

    @Override
    public Page<paperRuleEntity> findPaperRules(
            Long paperRuleId,
            String subject,
            Integer currentPage,
            Integer pageSize) {

        LambdaQueryWrapper<paperRuleEntity> wrapper = Wrappers.lambdaQuery(paperRuleEntity.class);
        if (paperRuleId != null) {
            wrapper.eq(paperRuleEntity::getPaperRuleId, paperRuleId);
            return super.page(new Page<>(currentPage, pageSize), wrapper);
        }
        if(subject != null){
            wrapper.eq(paperRuleEntity::getSubject, subject);
            return super.page(new Page<>(currentPage, pageSize), wrapper);
        }
        else {
            return new Page<>(currentPage, pageSize);
        }
    }

    @Override
    public int getScoreByExamId(Long examId) {
        Exam exam = examRepository.findByExamId(examId);
        Long PaperRuleId = exam.getPaperRuleId();
        paperRuleEntity paperRule = paperRuleRepository.getById(PaperRuleId);
        return paperRule.getScoreChoice();
    }

    @Override
    public int getNumByExamId(Long examId) {
        Exam exam = examRepository.findByExamId(examId);
        Long PaperRuleId = exam.getPaperRuleId();
        paperRuleEntity paperRule = paperRuleRepository.getById(PaperRuleId);
        return  paperRule.getNumMulChoice()+paperRule.getNumSinChoice();
    }

}